﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Prsrp
{
    public class DrawingColors
    {
        private static Texture2D mpRedDot;
        private static Texture2D mpWhiteDot;

        private static Texture2D GetTexture2DFromColor(Color _kColor)
        {
            Texture2D pNewTexture = new Texture2D(Game.Instance.GraphicsDevice, 1, 1);
            pNewTexture.SetData<Color>(new Color[] { _kColor });
            return pNewTexture;
        }

        static DrawingColors()
        {
            mpRedDot = GetTexture2DFromColor(Color.Red);
            mpWhiteDot = GetTexture2DFromColor(Color.White);
        }

        public static Texture2D Red { get { return mpRedDot; } }
        public static Texture2D White { get { return mpWhiteDot; } }
    }

    /// <summary>
    /// This static class containt various tools for interacting with SpriteBatch.
    /// You can access current SpriteBatch at: "Game.Instance.SpriteBatch".
    /// </summary>
    static class DrawingFunctions
    {
        public static Texture2D mpRedDot;

        public static void DrawHorizontalLine(int _iStartX, int _iStartY, int _iEndX)
        {
            for (int i = _iStartX; i <= _iEndX; ++i)
            {
                Game.Engine.SpriteBatch.Draw(mpRedDot, new Vector2(i, _iStartY), Color.White);
            }
        }

        public static void DrawVerticalLine(int _iStartX, int _iStartY, int _iEndY)
        {
            for (int i = _iStartY; i <= _iEndY; ++i)
            {
                Game.Engine.SpriteBatch.Draw(mpRedDot, new Vector2(_iStartX, i), Color.White);
            }
        }

        public static void DrawPoint(Vector2 _vPos, Texture2D _pPixel)
        {
            Game.Engine.SpriteBatch.Draw(_pPixel, _vPos, Color.White);
        }

       
        /// <summary>
        /// Draw a line from two vectors.
        /// </summary>
        public static void DrawLine(Vector2 _vLineStart, Vector2 _vLineEnd, Texture2D _pPixelColor)
        {
            float iLengthX = _vLineEnd.X - _vLineStart.X;
            float iLengthY = _vLineEnd.Y - _vLineStart.Y;
            float iAbsLengthX = Math.Abs(iLengthX);
            float iAbsLengthY = Math.Abs(iLengthY);
            float iStepCount = (iAbsLengthX > iAbsLengthY) ? iAbsLengthX : iAbsLengthY;
            float iStepSizeX = iLengthX / iStepCount;
            float iStepSizeY = iLengthY / iStepCount;
            for (int i = 0; i < iStepCount; ++i)
            {
                float iCurX = _vLineStart.X + iStepSizeX * i;
                float iCurY = _vLineStart.Y + iStepSizeY * i;
                //Game.Engine.SpriteBatch.Draw(mpRedDot, new Vector2(iCurX, iCurY), Color.White);
                DrawPoint(new Vector2(iCurX, iCurY), _pPixelColor);
            }
        }

        public static void DrawRectangle(Rectangle _pRect, Texture2D _pPixelColor)
        {
            int iLeft = _pRect.Left;
            int iRight = _pRect.Right;
            int iTop = _pRect.Top;
            int iBottom = _pRect.Bottom;

            DrawLine(new Vector2(iLeft, iTop), new Vector2(iRight, iTop), _pPixelColor);
            DrawLine(new Vector2(iLeft, iBottom), new Vector2(iRight, iBottom), _pPixelColor);
            DrawLine(new Vector2(iLeft, iTop), new Vector2(iLeft, iBottom), _pPixelColor);
            DrawLine(new Vector2(iRight, iTop), new Vector2(iRight, iBottom), _pPixelColor);
        }
    }
}
